<script>
  function ajax(url, method = "GET", params) {
    const p = new Promise((resolve, reject) => {
      const xhr = new XMLHttpRequest();
      xhr.open(method, url, true);
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4) {
          if (xhr.status === 200) {
            resolve(JSON.parse(xhr.responseText));
          } else if (xhr.status === 404) {
            reject(new Error("404, not found"));
          } else if (xhr.status === 500) {
            reject(new Error("500, server error"));
          }
        }
      };
      xhr.send(JSON.stringify(params));
    });
    return p;
  }

  ajax("../data/ajax.json")
    .then((data) => console.log(data))
    .catch((error) => console.log(error));
</script>
